/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 * <p>
 * PmCalendarController
 * 项目日历
 * <p>
 * 修改纪录
 * 2020-05-16 版本：1.0 wushenghong 创建。
 *
 * @version 版本：1.0
 * @author 作者：wushenghong
 * 创建日期：2020-05-16
 */

package com.topscomm.pm.controller;

import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.cbo.model.TreeModel;
import com.topscomm.pm.controller.auto.PmCalendarControllerAuto;
import com.topscomm.pm.utils.MisStringUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.common.TapSystemConst;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: wushenghong
 * @date: 2020-05-16
 * @description:项目日历Controller
 * @modify:
 */
@RestController
@RequestMapping("pm/calendar")
public class PmCalendarController extends PmCalendarControllerAuto implements IPmCalendarController {
    /**
     * @param paraMap
     * @return
     * @author: wushenghong
     * @date: 2020-05-16
     * @description:转换出查询SQL语句
     * @modify:
     */
    @Override
    protected String spellListSql(Map<String, Object> paraMap) {
        StringBufferProxy sql = new StringBufferProxy();
//        try {
//            this.service.insertBatchCalendar(paraMap);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        sql.appendSingle(" 1=1 ");
        if (paraMap.containsKey("qry_projectid")) {
            String value = paraMap.get("qry_projectid").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and projectid = '{0}' ", value);
        }
        if (paraMap.containsKey("qry_startdate")) {
            String value = paraMap.get("qry_startdate").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and calendardate >= {0} ", SQLAdapter.toDateLong(value + " 00:00:00"));
        }
        if (paraMap.containsKey("qry_enddate")) {
            String value = paraMap.get("qry_enddate").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and calendardate <= {0}", SQLAdapter.toDateLong(value + " 23:59:59"));
        }
        if (this.getUserBean().getUser().getAdminlevel() < 1)// 管理员、超级管理员，不控制权限
        {
            String dataPession = service.getDataPermission(TapSystemConst.TapOperationTypeEnum.Select, "", this.getUserBean());
            if (StringUtil.isEmpty(dataPession)) {
                sql.appendSingle(" and " + MisStringUtil.createWhereByUsercodeFromProject(this.getUserBean().getUser(), "projectid", service.getDao().getTableName()));
            } else// 使用数据权限
            {
                sql.appendSingle(" and {0} ", dataPession);
            }
        }
        return sql.toString();
    }

    /**
     * @param paraMap
     * @return
     * @description: 参照查询方法
     * @author: wushenghong
     * @date: 2020-05-16
     * @modify:
     */
    @Override
    @GetMapping(value = "/refList")
    public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
        return super.refList(paraMap);
    }

    /**
     * @param paraMap
     * @return
     * @description: 树查询方法
     * @author: wushenghong
     * @date: 2020-05-16
     * @modify:
     * @GetMapping(value = "/queryTreeList")
     */
    @GetMapping(value = "/queryTreeList")
    public ResponseResult<Object> queryTreeList(@RequestParam(required = false) Map<String, Object> paraMap) {
        String sql =" select id,code,name from pmproject where projectstate in('1','2','3','4')";
        List<Map<String, Object>> listModule = this.service.selectExecute(sql);
//        //去重
//        listModule = listModule.stream().collect(
//                Collectors.collectingAndThen(
//                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(item -> ConvertUtil.convertToString(item.get("projectid"))))), ArrayList::new));
        for (Map<String, Object> item : listModule) {
            item.put("parentid", 0);
            item.put("code", item.get("code"));
            item.put("id", item.get("id"));
            item.put("name", item.get("name"));
        }
        List<Map<String, Object>> listNewModule = new ArrayList<Map<String, Object>>(listModule);
        TreeModel treeModel = new TreeModel();
        List<TreeModel> treeList = treeModel.buildTree(listNewModule, true, "项目列表");
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("dataList", treeList);
        return ResponseResult.ok(dataMap);
    }

    /**
     * @param paraMap
     * @return
     * @author: wushenghong
     * @date: 2020-05-09
     * @description:项目日历生成接口
     * @modify:
     */
    public ResponseResult<Object> batchAddCalender(@RequestBody(required = false) Map paraMap) {
        try {
            this.service.insertBatchCalendar(paraMap);
            return ResponseResult.ok("项目日历生成成功");
        } catch (Exception ex) {
            return handleControllerException(ex);
        }
    }
}

